package com.example.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api")
public class HystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${consumer.url}")
    private String consumerUrl;

    @GetMapping("/hello")
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity(consumerUrl + "api/hello", String.class, "");
        String body = forEntity.getBody();
        return body;
    }

    public String helloError() {
        return "error hello";
    }
}
